﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TB_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入用户名", "警告");
                textBox1.Focus();
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("请输入密码", "警告");
                textBox2.Focus();
            }
            else if(textBox1.Text=="LY" && textBox2.Text=="LY")
            {
                Class.SysInfo.INSERT_VALUE("Server", "LY");
                MessageBox.Show("服务器:LY 切换成功,软件即将关闭，请重新打开");
            }
            else if (textBox1.Text == "59igou" && textBox2.Text == "59igou")
            {
                Class.SysInfo.INSERT_VALUE("Server", "59igou");
                MessageBox.Show("服务器:59igou 切换成功,软件即将关闭，请重新打开");
            }
            else
            {
                //执行账号密码判断
                int Type_Conform = Class.SysInfo.WSSUSER.CONFORM_LOGIN_VER(
                    Convert.ToInt32(Class.SysInfo.INFO_Seller_ID.ToString()),
                    textBox1.Text.Trim().ToString(),
                    textBox2.Text.Trim().ToString(), Class.SysInfo.Get_Version().ToString());
                if (Type_Conform == 0)
                {
                    Class.SysInfo.S_User_Name = textBox1.Text.Trim().ToString();
                    Class.SysInfo.S_U_Password = textBox2.Text.Trim().ToString();
                    if (Class.SysInfo.Get_Rate_Show() == "True")
                    {
                        Main_Rate MR = new Main_Rate();
                        MR.Show();
                    }
                    else
                    {
                        TB_CS_MainForm TCMF = new TB_CS_MainForm();
                        TCMF.Show();
                    }
                    if(checkBox1.Checked)
                    {
                        Class.SysInfo.INSERT_VALUE("UserName", textBox1.Text.Trim().ToString());
                        Class.SysInfo.INSERT_VALUE("UserPWD", textBox2.Text.Trim().ToString());
                        
                    }
                    else
                    {
                        Class.SysInfo.INSERT_VALUE("UserName", "");
                        Class.SysInfo.INSERT_VALUE("UserPWD", "");
                    }

                    this.Visible = false;
                }
                else if(Type_Conform==2)
                {
                    MessageBox.Show("当前版本已过期，请联系管理员或主管更新版本", "警告");
                }
                else
                {
                    MessageBox.Show("账号或密码错误，并检查店铺设置 当前店铺为：" + Class.SysInfo.GET_Seller_Nick().ToString(), "警告");
                }
            }
        }
        private static DataSet SELLER_DTS;
        private void Form1_Load(object sender, EventArgs e)
        {
            if (Class.SysInfo.GET_Seller_Nick() == "")
            {
                //空的
                Plug.Setting_User PSU = new Plug.Setting_User();
                PSU.Show();
            }
            else
            {
                this.Text = "当前服务器：" + Class.SysInfo.Get_Server();
                if (Class.SysInfo.Get_UserName() != "")
                {
                    textBox1.Text = Class.SysInfo.Get_UserName().Trim().ToString();
                    textBox2.Text = Class.SysInfo.Get_UserPWD().Trim().ToString();
                    checkBox1.Checked = true;
                }
                if (Class.SysInfo.Get_Server() == "LY")
                {
                    Class.SysInfo.WSEE.Url = "http://ly.59igou.com/ASMX/event.asmx";
                    Class.SysInfo.WSHB.Url = "http://ly.59igou.com/ASMX/HongBao_Logic.asmx";
                    Class.SysInfo.WSSELLER.Url = "http://ly.59igou.com/ASMX/seller_manage.asmx";
                    Class.SysInfo.WSSUSER.Url = "http://ly.59igou.com/ASMX/Seller_User.asmx";
                    Class.SysInfo.WS_TOP.Url = "http://ly.59igou.com/ASMX/TB_Top.asmx";
                    Class.SysInfo.WSRATE.Url = "http://ly.59igou.com/ASMX/Rate_Manage.asmx";

                }
                else
                { }
                Thread Check_Seller_Nick_TD = new Thread(new ThreadStart(Check_Seller_Nick));

                Check_Seller_Nick_TD.Start();
            }
            
        }

        private void Check_Seller_Nick()
        {
            string Seller_Nick = Class.SysInfo.GET_Seller_Nick();
            SELLER_DTS = Class.SysInfo.WSSELLER.GET_SELLER_INFO(Seller_Nick);
            if (SELLER_DTS.Tables[0].Rows.Count == 0)
            {
                //不存在这个卖家账号
                MessageBox.Show("卖家账号设置异常，请检查CS_Config.xml 程序即将自动关闭", "警告");
                Application.Exit();
            }
            else
            {
                Class.SysInfo.INFO_Seller_ID = SELLER_DTS.Tables[0].Rows[0][0].ToString();
                Class.SysInfo.INFO_Seller_Nick = SELLER_DTS.Tables[0].Rows[0][1].ToString();
                Class.SysInfo.Info_Seller_PWD = SELLER_DTS.Tables[0].Rows[0][2].ToString();
                button1.Enabled = true;
            }
        }

    
        
    }
}
